package elitech.vietnam.myfashion.view;

import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import elitech.vietnam.myfashion.LoginActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.MyApplication;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.dialog.ChangePasswordDialog;
import elitech.vietnam.myfashion.dialog.EditShippingDialog;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.extend.ViewPagerAdapter;
import elitech.vietnam.myfashion.fragment.EditProfileFragment;
import elitech.vietnam.myfashion.fragment.FollowListFragment;

public class SettingView extends LinearLayout implements OnClickListener {
	public static final int LOGINACTIVITY = 1;
	private MainActivity mContext;
	private Button mBtnLogin;
	private TextView mTxtLanguage, mTxtVersion;
	private LinearLayout mBtnEditProfile, mBtnFriendList, mBtnChangePassword,
			mBtnShippingAddress, mBtnGetNotification, mBtnLanguage, mBtnHomePage;
	private ToggleButton mToggle;
	private LinearLayout mMainMenu;

	public SettingView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.setting, this);
		}

		mBtnLogin = (Button) findViewById(R.id.setting_login);
		mBtnEditProfile = (LinearLayout) findViewById(R.id.setting_btneditprofile);
		mBtnFriendList = (LinearLayout) findViewById(R.id.setting_btnfriendlist);
		mBtnChangePassword = (LinearLayout) findViewById(R.id.setting_btnchangepassword);
		mBtnShippingAddress = (LinearLayout) findViewById(R.id.setting_btnshippingaddress);
		mBtnGetNotification = (LinearLayout) findViewById(R.id.setting_btngetnotification);
		mToggle = (ToggleButton) findViewById(R.id.setting_togglenotification);
		mBtnLanguage = (LinearLayout) findViewById(R.id.setting_btnlanguage);
		mBtnHomePage = (LinearLayout) findViewById(R.id.setting_btnHomePage);
		mTxtLanguage = (TextView) findViewById(R.id.setting_txtlanguage);
		mTxtVersion = (TextView) findViewById(R.id.setting_txtVersion);
		mMainMenu = (LinearLayout) findViewById(R.id.main_menu);
		mMainMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});
		
		mBtnLogin.setOnClickListener(this);
		mBtnEditProfile.setOnClickListener(this);
		mBtnFriendList.setOnClickListener(this);
		mBtnChangePassword.setOnClickListener(this);
		mBtnShippingAddress.setOnClickListener(this);
		mBtnGetNotification.setOnClickListener(this);
		mBtnLanguage.setOnClickListener(this);
		mBtnHomePage.setOnClickListener(this);
		mTxtLanguage.setText(mContext.getLocale().getDisplayLanguage());
		
		if (mContext.mUser != null)
			mBtnLogin.setText(mContext.getString(R.string.signout));
		mToggle.setChecked(mContext.GetPreferenceBool("GetNotification"));
		
		try {
			PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
			mTxtVersion.setText(pInfo.versionName);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	
	}

	public void onResume() {
		if (mContext.mUser != null)
			mBtnLogin.setText(mContext.getString(R.string.signout));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_login:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				mContext.mOrder = new Order();
				mContext.mUser = null;
				mContext.AddPreference("User", "");
				mBtnLogin.setText(mContext.getString(R.string.login));
				mContext.mMainPageView.mPageAdapter = new ViewPagerAdapter(
						mContext);
				mContext.mListProductBest.clear();
				mContext.mListMyStyles.clear();
				mContext.mMainPageView.mViewPager
						.setAdapter(mContext.mMainPageView.mPageAdapter);
				mContext.mMainPageView.mViewPager.setCurrentItem(3, false);
				((MyApplication) mContext.getApplicationContext()).stopGCMNotification();
				Toast.makeText(mContext, mContext.getString(R.string.logoutsuccess), Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.setting_btneditprofile:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				if (mContext.mEditProfileFragment == null)
					mContext.mEditProfileFragment = new EditProfileFragment();
				if (!mContext.mEditProfileFragment.isAdded()) {
					mContext.replaceFragment(mContext.mEditProfileFragment, true);
				}
			}
			break;

		case R.id.setting_btnfriendlist:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				if (mContext.mFollowListFragment == null)
					mContext.mFollowListFragment = new FollowListFragment();
				if (!mContext.mFollowListFragment.isAdded()) {
					mContext.replaceFragment(mContext.mFollowListFragment, true);
				}
			}
			break;

		case R.id.setting_btnchangepassword:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				new ChangePasswordDialog(mContext).show();
			}
			break;

		case R.id.setting_btnshippingaddress:
			new EditShippingDialog(mContext).show();
			break;
		
		case R.id.setting_btnlanguage:
			new Dialog(mContext).show();
			break;
			
		case R.id.setting_btnHomePage:
			Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.fashionhanquoc.com"));
			mContext.startActivity(browserIntent);
			break;
			
		case R.id.setting_btngetnotification:
			mToggle.performClick();
			if (mToggle.isChecked() && mContext.mUser!=null) 
				((MyApplication) mContext.getApplicationContext()).startGCMNotification();
			else
				((MyApplication) mContext.getApplicationContext()).stopGCMNotification();
			mContext.AddPreferenceBool("GetNotification", mToggle.isChecked());
			break;
		}

	}

	public SettingView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SettingView(Context context) {
		super(context);
	}

	private class Dialog extends android.app.Dialog implements
			View.OnClickListener {

		public Dialog(Context context) {
			super(context, R.style.Dialog);
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.dl_language);

			this.findViewById(R.id.dl_language_EN).setOnClickListener(this);
			this.findViewById(R.id.dl_language_VN).setOnClickListener(this);
			this.findViewById(R.id.dl_language_KR).setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			Intent i = new Intent(mContext, MainActivity.class);
			switch (v.getId()) {
			case R.id.dl_language_EN:
				setDefaultLocale(getContext().getApplicationContext(), Locale.ENGLISH);
				mContext.startActivity(i);
				mContext.finish();
				break;

			case R.id.dl_language_VN:
				setDefaultLocale(getContext().getApplicationContext(), new Locale("vi_VN"));
				mContext.startActivity(i);
				mContext.finish();
				break;
				
			case R.id.dl_language_KR:
				setDefaultLocale(getContext().getApplicationContext(), Locale.KOREA);
				mContext.startActivity(i);
				mContext.finish();
				break;
			}
		}
		
		public void setDefaultLocale(Context context, Locale locale) {
	        Locale.setDefault(locale);
	        Configuration appConfig = new Configuration();
	        appConfig.locale = locale;
	        context.getResources().updateConfiguration(appConfig,
	                context.getResources().getDisplayMetrics());
	        System.out.println("trad" + locale.getLanguage());
	    } 
		
	}

}
